﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace BLL.Utils
{
    public class Utils
    {

        public static string AdjustDateTime(string paramp, int Omda)
        {
            char[] splitter1 = { ' ' };
            char[] splitterD = { '/' };
            char[] splitterH = { ':' };
            string saida = "";

            string[] auxp = paramp.Split(splitter1);
            string[] auxpD = auxp[0].Split(splitterD);
            string[] auxpH = new string[3];
            if (auxpD.Length != 3)
            {
                splitterD[0] = '-';
                auxpD = auxp[0].Split(splitterD);
                if (auxpD.Length != 3)
                {
                    splitterD[0] = '.';
                    auxpD = auxp[0].Split(splitterD);
                    if (auxpD.Length != 3)
                    {
                        throw new FormatException();
                    }
                }
            }
            if (auxpD.Length == 3)
            {
                for (int i = 0; i <= 2; i++)
                {
                    if (auxpD[i].Length == 1)
                    {
                        auxpD[i] = "0" + auxpD[i];
                    }
                }
                saida = auxpD[2] + "/" + auxpD[1] + "/" + auxpD[0];
            }
            if (auxp.Length > 1)
            {
                auxpH = auxp[1].Split(splitterH);
                if (auxpH.Length == 3)
                {
                    for (int i = 0; i <= 2; i++)
                    {
                        if (auxpH[i].Length == 1)
                        {
                            auxpH[i] = "0" + auxpH[i];
                        }
                    }
                    if (auxp.Length > 2 && Omda == 0)
                    {
                        int auxHPM = Convert.ToInt32(auxpH[0]);
                        if (auxp[2] == "PM")
                        {
                            if (auxHPM != 12 && auxHPM != 0)
                            {
                                auxHPM += 12;
                            }
                            auxpH[0] = auxHPM.ToString();
                        }
                        else
                        {
                            if (auxHPM == 12)
                            {
                                auxpH[0] = "00";
                            }
                        }
                    }
                    if (Omda == 0)
                    {
                        saida += " " + auxpH[0] + ":" + auxpH[1] + ":" + auxpH[2];
                    }
                }
                else
                {
                    throw new FormatException();
                }
            }
            if (saida == "01/01/1900 00:00:00")
            {
                saida = "";
            }
            return saida;
        }

        public string pMoneyToIMoney(string moneyp)
        {
            moneyp = moneyp.Replace(",","#");
            moneyp = moneyp.Replace("#",".");
            return moneyp;
        }

        public bool ValidaNumeroComVirgula(string numero)
        {
            Regex rx = new Regex(@"^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$");
            return rx.IsMatch(numero);
        }

        public bool ValidaLetrasNumeros(string texto)
        {
            Regex rx = new Regex("^[a-zA-ZÀ-Üà-ü0-9\\r\\n?!,. ]*$");
            return rx.IsMatch(texto);
        }    


    }
}